/*林尽水源，便得一山，山有小口，仿佛若有光。*/
package com.lib.base.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.lib.base.ext.dp2px

/**
 * @name  : ProtectedViewDialog
 * @author: create by  itzhu |  2021/12/23
 * @desc  :显示协议
 */
class DialogHelper {

    private var dialog: Dialog
    private var context: Context
    private var view: View? = null

    constructor(context: Context, defaultStyle: Boolean = true) {
        this.context = context
        this.dialog = BaseDialogNoBinding(context)
        if (defaultStyle) {
            defaultStyle()
        }
    }

    fun defaultStyle(): DialogHelper {
        val padding = context.dp2px(24f).toInt()
        setDialogStyle(useDefaultPadding = false, paddingLeft = padding, paddingRight = padding)
        return this
    }

    fun setPaddingLeftRightUseDp(paddingDp: Int): DialogHelper {
        val padding = context.dp2px(paddingDp.toFloat()).toInt()
        setDialogStyle(useDefaultPadding = false, paddingLeft = padding, paddingRight = padding)
        return this
    }

    /**
     * @param useDefaultPadding
     * true-使用系统dialog自带默认的padding。
     * false-使用[paddingLeft],[paddingTop],[paddingRight],[paddingBottom]
     * */
    fun setDialogStyle(
        windowWidth: Int? = null,
        windowHeight: Int? = null,
        windowGravity: Int? = null,
        windowDimAmount: Float? = null,
        windowAnimations: Int? = null,
        useDefaultPadding: Boolean? = null,
        paddingLeft: Int? = null,
        paddingTop: Int? = null,
        paddingRight: Int? = null,
        paddingBottom: Int? = null,
    ): DialogHelper {
        (dialog as BaseDialogNoBinding).apply {
            windowWidth?.let { this.windowWidth = it }
            windowHeight?.let { this.windowHeight = it }
            windowGravity?.let { this.windowGravity = it }
            windowDimAmount?.let { this.windowDimAmount = it }
            windowAnimations?.let { this.windowAnimations = it }
            useDefaultPadding?.let { this.useDefaultPadding = it }
            paddingLeft?.let { this.paddingLeft = it }
            paddingTop?.let { this.paddingTop = it }
            paddingRight?.let { this.paddingRight = it }
            paddingBottom?.let { this.paddingBottom = it }
        }
        return this
    }

    fun setCancelable(cancelable: Boolean): DialogHelper {
        dialog.setCancelable(cancelable)
        return this
    }

    fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): DialogHelper {
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
        return this
    }

    fun setView(view: View, configView: (view: View, dialog: Dialog) -> Unit): DialogHelper {
        this.view = view
        (dialog as BaseDialogNoBinding).setView(view)
        configView(view, dialog)
        return this
    }

    fun setView(viewId: Int, configView: (view: View, dialog: Dialog) -> Unit): DialogHelper {
        this.view = LayoutInflater.from(context).inflate(viewId, null, false)
        (dialog as BaseDialogNoBinding).setView(view!!)
        configView(this.view!!, dialog)
        return this
    }

    fun show(): DialogHelper {
        dialog.show()
        return this
    }

    fun dismiss() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }

    fun getDialog() = dialog

    fun isShowing() = dialog.isShowing

    fun getTextView(id: Int): TextView? {
        if (view == null) return null
        return try {
            view?.findViewById(id)
        } catch (e: Exception) {
            null
        }
    }
}